using System.Collections;
using Curriculum.Services;
using Utiles;

namespace ConcursoCV.UI.WebUserControls.Concurso
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;

	/// <summary>
	///		Summary description for ListadoPostulantes.
	/// </summary>
	public class ListadoPostulantes : System.Web.UI.UserControl
	{
		protected System.Web.UI.WebControls.DataGrid dtgPostulantes;
		protected System.Web.UI.WebControls.Button btnVolver;
		protected System.Web.UI.WebControls.Label lblCount;

		private const string pidListadoConcursos = "006";
		private const string pidCalificaciones = "010";

		private void Page_Load(object sender, System.EventArgs e)
		{
			ChequearPermisos();
			if(!this.IsPostBack)
			{
				ConcursoId = this.Request["concursoId"];
				InitializeVariables();
				FillDataGridWithSearch();
			}
		}

		private void ChequearPermisos()
		{
			if( Helper.SecurityHelper.EsAdminitrador() || Helper.SecurityHelper.EsRoleGenerico() )
			{
				dtgPostulantes.Columns[4].Visible = false;
			}
			if( Helper.SecurityHelper.EsJurado() )
			{
				dtgPostulantes.Columns[3].Visible = false;
			}
		}

		private void InitializeVariables()
		{
			SortColumn = "Nombre";
			SortDirection = true;			
		}

		private IList GetListBySearch() 
		{
			// Search			
			ConcursoService service = new ConcursoService();
			return service.ObtenerPostulantes( ConcursoId );
		}
		private void FillDataGridWithSearch()
		{
			IList list = GetListBySearch();
			((ArrayList)list).Sort(new GenericComparer(SortColumn,SortDirection));
			dtgPostulantes.DataSource = list;
			lblCount.Text = list.Count.ToString();
			try
			{
				dtgPostulantes.DataBind();
			}
			catch(Exception exc )
			{
				string msg = exc.Message;
				dtgPostulantes.CurrentPageIndex = 0;
				FillDataGridWithSearch();
			}

			list = null;
		}

		private void RedirigirACalificaciones(string postulanteId)
		{			
			string url = "Default.aspx?pid=" + pidCalificaciones + "&concursoId=" + ConcursoId + "&docenteId=" + postulanteId + GetParametroTipo();
			Server.Transfer(url);
		}

		private string GetParametroTipo()
		{
			string tipoParam = string.Empty;
			if (this.Request["type"] != null)
			{
				tipoParam = "&type=" + this.Request["type"];
			}
			return tipoParam;
		}

		private void RedirigirAListadoConcursos()
		{
			Redirigir(pidListadoConcursos);
		}
		private void Redirigir(string pid)
		{
			string url = "Default.aspx?pid=" + pid + GetParametroTipo();
			Server.Transfer(url);
		}

		#region - properties -


		public string ConcursoId
		{
			get{ return (string)ViewState["_concursoId"];}
			set{ ViewState["_concursoId"] = value;}
		}

		#region - Sort Variables -

		public string SortColumn
		{
			get{ return (string)ViewState["SortColumn"];}
			set{ ViewState["SortColumn"] = value;}
		}
		public bool SortDirection
		{
			get{ return (bool)ViewState["SortDirection"];}
			set{ ViewState["SortDirection"] = value;}
		}

		#endregion - Sort Variables -

		#endregion - properties -

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.dtgPostulantes.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dtgPostulantes_ItemCommand);
			this.dtgPostulantes.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.dtgPostulantes_PageIndexChanged);
			this.dtgPostulantes.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.dtgPostulantes_SortCommand);
			this.btnVolver.Click += new System.EventHandler(this.btnVolver_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void dtgPostulantes_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			if ( e.CommandName != "Page" )
			{
				string postulanteId = e.Item.Cells[0].Text;
				switch ( e.CommandName )
				{
					case "Ver":
							RedirigirACalificaciones(postulanteId);
						break;

					case "Calificar":
							RedirigirACalificaciones(postulanteId);
						break;
				}
			}				
		}

		private void btnVolver_Click(object sender, System.EventArgs e)
		{
			RedirigirAListadoConcursos();
		}

		private void dtgPostulantes_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
		{
			dtgPostulantes.CurrentPageIndex = e.NewPageIndex;
			FillDataGridWithSearch();
		}

		private void dtgPostulantes_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
		{
			//Si se hace click sobre ,la misma columna, cambia el orden
			if (SortColumn.ToString() == e.SortExpression.ToString())
				SortDirection = !SortDirection;
			
			SortColumn = e.SortExpression;
			FillDataGridWithSearch();		
		}
	}
}
